﻿using GameView.Models;
using System.Net.Http;
using System.Net.Http.Json;

namespace GameView.Services.Comment
{
    public class HttpService : IHttpService
    {
        private readonly HttpClient _httpClient;

        public HttpService(HttpClient httpClient)
        {
            _httpClient = httpClient;
            _httpClient.BaseAddress = new Uri("https://api.example.com/");
            _httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
        }

        public async Task<ApiResponse<T>> DeleteAsync<T>(string url)
        {
            var response = await _httpClient.DeleteFromJsonAsync<ApiResponse<T>>(url);
            return response!;
        }

        public async Task<ApiResponse<T>> GetAsync<T>(string url)
        {
            var response = await _httpClient.GetFromJsonAsync<ApiResponse<T>>(url);
            return response!;
        }

        public async Task<ApiResponse<T>> PostAsync<T>(string url, object data)
        {
            var response = await _httpClient.PostAsJsonAsync(url, data);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadFromJsonAsync<ApiResponse<T>>()!;
        }

        public async Task<ApiResponse<T>> PutAsync<T>(string url, object data)
        {
            var response = await _httpClient.PutAsJsonAsync(url, data);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadFromJsonAsync<ApiResponse<T>>()!;
        }
    }
}